package cz.andsolutiondroid.model;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.json.JSONException;
import org.json.JSONObject;


public class Reality {
	private String id;
	private String name;
	private String area; 
	private String price;
	private int synchronize;
	
	public Reality(String name, String area, String price) {
		this.name = name;
		this.area = area;
		this.price = price;
	}
	
	public Reality(String id, String name, String area, String price, int sync) {
		this.id = id;
		this.name = name;
		this.area = area;
		this.price = price;
		this.synchronize = sync;
	}
	
	public Reality() {
		// TODO Auto-generated constructor stub
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String toString(){
		
		return getName();
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public void setSynchronize(int synchronize) {
		this.synchronize = synchronize;
	}

	public int getSynchronize() {
		return synchronize;
	}
	
	public String getHash(){
		
		String string = this.getName() + this.getArea() + this.getPrice();
		
		return this.md5(string);
	}
	
	private String md5(String s) {
	    try {
	        // Create MD5 Hash
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes());
	        byte messageDigest[] = digest.digest();
	        
	        // Create Hex String
	        StringBuffer hexString = new StringBuffer();
	        for (int i=0; i<messageDigest.length; i++)
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        return hexString.toString();
	        
	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    }
	    return "";
	}
	
	public Object toJsonString() {
		
		JSONObject obj = new JSONObject();
	    try {
			obj.put("name", getName());
			obj.put("area", getArea());
			obj.put("price", getPrice());
			obj.put("hash", getHash());
			
			return obj.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    return null;
	}
	
}
